<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    :footer="null"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
   <a-spin :spinning="confirmLoading">
      <!-- 搜索区域-begin -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :xl="7" :lg="9" :md="10" :sm="24">
               <a-form-item label="时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
                 <a-range-picker
                   style="width: 260px"
                   v-model="queryParam.createTimeRange"
                   format="YYYY-MM-DD"
                   :placeholder="['开始时间', '结束时间']"
                   @ok="onDateOk"
                   @change="onDateChange"
                 />
               </a-form-item>
              </a-col>
              <a-col :xl="14" :lg="11" :md="14" :sm="24">
                <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                  <a-button v-if="showTab == 1" type="primary" @click="searchQuery" icon="search">查询</a-button>
                  <a-button v-else type="primary" @click="searchChart" icon="search">查询</a-button>
                  <a-button v-if="showTab == 1" type="primary" @click="searchReset1" icon="reload" style="margin-left: 8px">重置</a-button>
                  <a-button v-else type="primary" @click="reload2" icon="reload" style="margin-left: 8px">重置</a-button>
                </span>
              </a-col>
              <a-col :xl="3" :lg="4" :md="8" :sm="24">
                <div class="extra-wrapper" style="padding-left: 0px;">
                <!-- <div class="extra-wrapper" style="padding-left: 50px;"> -->
                 <a-radio-group v-model="indexRegisterType" @change="changeRegisterType">
                    <a-radio-button value="1">列表</a-radio-button>
                    <a-radio-button value="0">图表</a-radio-button>
                  </a-radio-group>
                </div>
              </a-col>
            </a-row>
          </a-form>
        </div>
      <!-- 搜索区域-end -->
    <div v-show="showTab==1">
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @expand="expandRow"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
      </div>
     <div v-show="showTab!==1">
       <trend-chart ref="trendChart" @clearTime="searchReset1"></trend-chart>
     </div>
  </a-spin>
  </j-modal>
</template>

<script> 
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import TrendChart from '../TrendChart'
  import moment from 'moment'
  export default{
    name:'HistoryModal',
    mixins:[JeecgListMixin, mixinDevice],
    components:{ 
      TrendChart
    },
    data(){
      return {
        visible:false,
        width:1200,
        disableSubmit: false,
        indexRegisterType:"1",
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        title:'',
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'频率(Hz)',
            align:"center",
            dataIndex: 'freq'
          },
          {
            title:'有功功率(W)',
            align:"center",
            dataIndex: 'actp'
          },
          {
            title:'亮度',
            align:"center",
            dataIndex: 'bri'
          },
          {
            title:'实时电流(A)',
            align:"center",
            dataIndex: 'curr'
          },
          {
            title:'无功功率(W)',
            align:"center",
            dataIndex: 'reactp'
          },
          {
            title:'温度(摄氏度)',
            align:"center",
            dataIndex: 'temper'
          },
          {
            title:'实时电压(V)',
            align:"center",
            dataIndex: 'volt'
          },
          {
            title:'设备SIM卡标识号',
            align:"center",
            dataIndex: 'imsi'
          },
          {
            title:'创建时间	',
            align:"center",
            dataIndex: 'createTime'
          }
        ],
        url: {
          list: "/corp/lampData/list",
          
        },
        // 查询条件
        queryParam: {
          createTimeRange:[],
          createTime_begin:'',
          createTime_end:'',
        },
        showTab:1, //控制显示列表还是图表
        date:null,
        date2:null,
      }
    },
    created() {
      this.initDate()
    },
    methods:{
      handleCancel () {
        this.close()
      },
      onDateOk(value) {
        console.log(value);
      },
      searchReset1() {
        this.initDate()
        this.loadData(1);
      },
      initDate(){
        let date = new Date()
        var year = new Date().getFullYear()
        var month = new Date().getMonth()
        let date2 = new Date(year,month,1)
        this.date2 = moment(date2).format('YYYY-MM-DD')
        this.date = moment(date).format('YYYY-MM-DD')
        this.queryParam.createTimeRange = [moment(this.date2),moment(this.date)]
        this.queryParam.createTime_begin = this.date2
        this.queryParam.createTime_end = this.date
      },
      open(e){
        this.visible = true
        this.$nextTick(()=>{
          this.$set(this.queryParam,'lampId',e.id)
          this.loadData()
       })
      },
      onDateChange(value, dateString) {
        this.queryParam.createTime_begin=dateString[0];
        this.queryParam.createTime_end=dateString[1];
      },
      reload2(){
        this.initDate()
        this.searchChart()
      },
      searchChart(){
        this.$refs.trendChart.getWeatherData(this.queryParam)
      },
      changeRegisterType(e){
        this.indexRegisterType = e.target.value
        if(this.indexRegisterType=="1"){
          this.showTab = 1
          this.loadData(1)
        }else{
          this.showTab = 0
          this.searchChart()
        }
      },
      // 展开显示详情
      expandRow(expanded,record){
        if(expanded){
          this.getHistoryData()
        }
      },
      // 获取照明历史数据
      getHistoryData(params){
        let that = this
       let httpurl = this.url.list
       that.confirmLoading = true;
        getAction(httpurl,params).then(res=>{
          if(res.success){
            that.weatherInfo = res.result
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      },       
      close () {
        this.visible = false;
      },
    }
  }
</script>

<style>
</style>